<template>
  <div class="app-container">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="群组编号：" prop="groupId" style="width: 200px;">
        <el-input v-model="form.groupId" disabled></el-input>
      </el-form-item>
      <el-form-item label="群组名称："  prop="groupName"  style="width: 500px;">
        <el-input v-model="form.groupName"></el-input>
      </el-form-item>
      <el-form-item label="创建者："   style="width: 200px;">
        <el-input v-model="form.teacherName" disabled></el-input>
      </el-form-item>
      <el-form-item label="群组描述：" prop="groupDescribe"   required>
        <el-input v-model="form.groupDescribe"></el-input>
      </el-form-item>
      <el-form-item label="最大人数：" style="width: 200px;">
        <el-input v-model="form.userMax"></el-input>
      </el-form-item>
      <el-form-item label="状态：" required>
        <el-select v-model="form.status" placeholder="状态">
          <el-option v-for="item in statusEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：" style="width: 300px;">
        <el-input v-model="form.createTime" disabled></el-input>
      </el-form-item>
      <el-form-item label="修改时间：" style="width: 300px;">
        <el-input v-model="form.modifyTime" disabled></el-input>
      </el-form-item>
      <el-form-item label="大学科：">
        <el-select v-model="form.dictionaryId" placeholder="学科"  clearable style="width: 80px;">
          <el-option v-for="item in mainSubject" :key="item[0]" :value="item[0]" :label="item[1]"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import groupApi from '@/api/group'
import questionApi from '@/api/question'

export default {
  data () {
    return {
      form: {
        groupId: null,
        groupName: '',
        groupDescribe: '',
        teacherId: null,
        teacherName: '',
        userMax: null,
        status: null,
        statusName: '',
        createTime: '',
        modifyTime: '',
        dictionaryId: null,
        dictionaryName: '',
        pageNum: 1,
        pageSize: 10
      },
      formLoading: false,
      rules: {
        groupName: [
          { required: true, message: '请输入群组名称', trigger: 'blur' }
        ],
        groupDescribe: [
          { required: true, message: '请输入群组描述', trigger: 'blur' }
        ]
      },
      mainSubject: []
    }
  },
  created () {
    this.typeChange()
    let id = this.$route.query.id
    console.log('id的值是：' + id)
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      _this.form.groupId = id
      groupApi.groupPageList(this.form).then(re => {
        console.log('接收到的数据是：' + re.response)
        _this.form = re.response.list[0]
        _this.formLoading = false
      })
    }
  },
  methods: {
    typeChange () {
      this.listLoading = true
      questionApi.selectType().then(data => {
        if (data.code === 1) {
          const re = data.response[0]
          this.mainSubject = re.mainSubject
        } else {
          console.error('获取数据失败，原因：', data.message)
        }
        this.listLoading = false
      }
      )
    },
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          groupApi.insertGroup(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/group/groups/list')
              })
            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        groupId: null,
        groupName: '',
        groupDescribe: '',
        status: null,
        teacherId: null,
        teacherName: '',
        userMax: null,
        createTime: '',
        modifyTime: '',
        dictionaryId: null
      }
      this.form.id = lastId
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      sexEnum: state => state.user.sexEnum,
      roleEnum: state => state.user.roleEnum,
      statusEnum: state => state.user.statusEnum
    })
  }
}
</script>
